/**
 * 熵巡 - 企业级氚云OpenAPI数据管理平台
 * 开发归属 Alyr.net
 * Toast通知组件
 * 尊重劳动成果是一种美德
 */

/**
 * 显示气泡提示
 * @param {string} message - 提示消息
 * @param {string} type - 提示类型：success(成功), error(失败), warning(警告), info(信息)
 * @param {number} duration - 显示时长（毫秒），默认3000ms
 */
function showToast(message, type = 'info', duration = 3000) {
    // 创建toast容器（如果不存在）
    let toastContainer = document.getElementById('toast-container');
    if (!toastContainer) {
        toastContainer = document.createElement('div');
        toastContainer.id = 'toast-container';
        toastContainer.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 10000;
            display: flex;
            flex-direction: column;
            gap: 10px;
            pointer-events: none;
        `;
        document.body.appendChild(toastContainer);
    }
    
    // 创建toast元素
    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    toast.style.cssText = `
        background-color: var(--color-bg-white);
        color: var(--color-text-dark);
        padding: 12px 20px;
        border-radius: var(--radius-md);
        border: 1px solid var(--color-border);
        min-width: 200px;
        max-width: 400px;
        font-size: var(--font-size-md);
        opacity: 0;
        transform: translateX(20px);
        transition: all 0.3s ease;
        pointer-events: auto;
        word-wrap: break-word;
    `;
    
    // 根据类型设置样式
    switch(type) {
        case 'success':
            toast.style.borderColor = '#dddddd';
            toast.style.backgroundColor = '#f5f5f5';
            toast.style.color = '#333333';
            message = '✓ ' + message;
            break;
        case 'error':
            toast.style.borderColor = '#dddddd';
            toast.style.backgroundColor = '#f9f9f9';
            toast.style.color = '#333333';
            message = '✗ ' + message;
            break;
        case 'warning':
            toast.style.borderColor = '#e0e0e0';
            toast.style.backgroundColor = '#f9f9f9';
            toast.style.color = '#666666';
            message = '⚠ ' + message;
            break;
        case 'info':
        default:
            toast.style.borderColor = '#dddddd';
            toast.style.backgroundColor = '#ffffff';
            toast.style.color = '#999999';
            message = 'ℹ ' + message;
            break;
    }
    
    toast.textContent = message;
    toastContainer.appendChild(toast);
    
    // 动画显示
    setTimeout(() => {
        toast.style.opacity = '1';
        toast.style.transform = 'translateX(0)';
    }, 10);
    
    // 自动隐藏
    setTimeout(() => {
        toast.style.opacity = '0';
        toast.style.transform = 'translateX(20px)';
        
        setTimeout(() => {
            if (toast.parentNode) {
                toastContainer.removeChild(toast);
            }
            
            // 如果容器为空，移除容器
            if (toastContainer.children.length === 0) {
                document.body.removeChild(toastContainer);
            }
        }, 300);
    }, duration);
}

// 快捷方法
const Toast = {
    success: (message, duration = 3000) => showToast(message, 'success', duration),
    error: (message, duration = 3000) => showToast(message, 'error', duration),
    warning: (message, duration = 3000) => showToast(message, 'warning', duration),
    info: (message, duration = 3000) => showToast(message, 'info', duration)
};
